package com.wlsk.license.common.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class ChineseToPinyinConverter {

    public static String toPinyin(String chinese) {
        StringBuilder pinyin = new StringBuilder();
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);

        char[] input = chinese.toCharArray();
        try {
            for (char c : input) {
                if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) { // 判断是否是中文字符
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(c, format);
                    if (temp != null) {
                        pinyin.append(temp[0]); // 取出第一个拼音
                    } else {
                        pinyin.append(c); // 如果不是中文字符，则原样输出
                    }
                } else {
                    pinyin.append(c); // 如果不是中文字符，则原样输出
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            badHanyuPinyinOutputFormatCombination.printStackTrace();
        }
        return pinyin.toString();
    }

    public static void main(String[] args) {
        JSONObject jsonObject = readJsonFile("C:\\Users\\mark\\Desktop\\需求\\综合出行\\平台包车V1.0.0\\cb_area_info.json");
        JSONArray array = jsonObject.getJSONArray("RECORDS");
        List<String> updateList = new ArrayList<>();
        for(int i=0; i<array.size(); i++) {
            JSONObject item = array.getJSONObject(i);
            String areaCode = item.getString("area_code");
            String name = item.getString("area_name");
            String pinyin = toPinyin(name);
            String initial = pinyin.substring(0, 1).toUpperCase();
            System.out.println(String.format("update cb_area_info set initial = '%s', pinyin = '%s' where area_code = '%s';", initial, pinyin, areaCode));
            updateList.add(String.format("update cb_area_info set initial = '%s', pinyin = '%s' where area_code = '%s';", initial, pinyin, areaCode));
        }
     //   writeLinesToFile("C:\\Users\\mark\\Desktop\\需求\\综合出行\\平台包车V1.0.0\\update_area.sql", updateList);
   //     System.out.println(jsonObject);
    }

    public static JSONObject readJsonFile(String filePath) {
        try {
            // 读取文件内容并转换为字符串
            String jsonString = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
            // 解析JSON字符串为JSONObject
            return JSON.parseObject(jsonString);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static boolean writeLinesToFile(String filePath, List<String> lines) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(filePath));
            // 遍历每一行数据并写入文件
            for (String line : lines) {
                bw.write(line);
                bw.newLine(); // 写入换行符
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}